package com.mxx.common.generator;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.*;

@Data
@Accessors(chain = true)
public class EntityAnno {
    private String annotationName;
    private String annotationClass;
    private String annotationPackage;
    private String annotationStr;
    private Set<AnnoValue> annotationValue;

    public EntityAnno(Class classes) {
        if (!classes.isAnnotation()) {
            throw new RuntimeException(classes.getName() + " is not annotation");
        }
        annotationName = classes.getSimpleName();
        annotationClass = classes.getName();
        annotationPackage = classes.getPackage().getName();
        annotationValue = new HashSet<>();
    }

    public EntityAnno afterPropertiesSet() {
        StringBuilder sb = new StringBuilder();
        sb.append("@").append(annotationName);
        if (annotationValue.size() > 0) {
            sb.append("(");
            for (AnnoValue annoValue : annotationValue) {
                sb.append(annoValue.toString()).append(", ");
            }
            sb.deleteCharAt(sb.length() - 1);
            sb.deleteCharAt(sb.length() - 1);
            sb.append(")");
        }
        this.annotationStr = sb.toString();
        return this;
    }

    public EntityAnno addValue(String key, String value) {
        this.annotationValue.add(AnnoValue.value(key, AnnoValue.Type.STRING, value));
        return this;
    }

    public EntityAnno addValue(String key, EntityAnno anno) {
        this.annotationValue.add(AnnoValue.value(key, AnnoValue.Type.ANNO, anno));
        return this;
    }

    public EntityAnno addValue(String key, Object value) {
        if (value instanceof String) {
            return addValue(key, (String) value);
        }
        this.annotationValue.add(AnnoValue.value(key, AnnoValue.Type.OTHER, value));
        return this;
    }

    public EntityAnno addValue(String key, String... value) {
        this.annotationValue.add(AnnoValue.value(key, AnnoValue.Type.ARRAY_STRING, value));
        return this;
    }

    public EntityAnno addValue(String key, EntityAnno... anno) {
        this.annotationValue.add(AnnoValue.value(key, AnnoValue.Type.ARRAY_ANNO, anno));
        return this;
    }

    public EntityAnno addValue(String key, Object... value) {
        if (value instanceof String[]) {
            return addValue(key, (String[]) value);
        }
        this.annotationValue.add(AnnoValue.value(key, AnnoValue.Type.ARRAY_OTHER, value));
        return this;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof EntityAnno) {
            return this.annotationName.equals(((EntityAnno) obj).annotationName);
        }
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return Objects.hash(annotationName);
    }
}
